//
//  taoCanAnLiVC.m
//  Wedding fantastic
//
//  Created by ZLM on 15/6/30.
//  Copyright (c) 2015年 dllo. All rights reserved.
//

#import "taoCanAnLiVC.h"

@interface taoCanAnLiVC ()

@end

@implementation taoCanAnLiVC
-(void)dealloc{
    [_taoVC release];
    [_anVC release];
    [super dealloc];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor greenColor];
    
    UISegmentedControl *segmentedControl = [ [ UISegmentedControl alloc ]
                                            initWithItems: nil ];
   
    segmentedControl.segmentedControlStyle = UISegmentedControlStyleBar;
    [ segmentedControl insertSegmentWithTitle:
     @"套餐" atIndex: 0 animated:YES];
    [ segmentedControl insertSegmentWithTitle:
     @"案例" atIndex: 1 animated:YES];
    segmentedControl.tintColor = [UIColor orangeColor];
    

    //改变segment的字体大小和颜色
    NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:[UIColor brownColor],NSForegroundColorAttributeName, nil];
    //设置各种状态的字体和颜色
    [segmentedControl setTitleTextAttributes:dict forState:UIControlStateNormal];

    

    [segmentedControl addTarget:self action:@selector(segmentAction:) forControlEvents:UIControlEventValueChanged];
    self.navigationItem.titleView = segmentedControl;

    /*
    // 第三方segmentedControl
    CCSegmentedControl *listSegmented = [[CCSegmentedControl alloc] initWithItems:@[@"套餐", @"案例"]];
    listSegmented.frame = CGRectMake(0, 64, 375 * kWidth / 375, 50 * kHeight / 667);
    listSegmented.layer.borderWidth = 0.5;
    listSegmented.layer.borderColor = [[UIColor orangeColor] CGColor];
    //设置背景图片，或者设置颜色，或者使用默认白色外观
    //    listSegmented.backgroundImage = [UIImage imageNamed:@"segment_bg.png"];
    listSegmented.backgroundColor = [UIColor whiteColor];
    
    //阴影部分图片，不设置使用默认椭圆外观的stain
    listSegmented.selectedStainView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"stain.png"]];
    listSegmented.selectedStainView = [[UIImageView alloc] init];
    listSegmented.selectedSegmentTextColor = [UIColor orangeColor];
    listSegmented.segmentTextColor = [self colorWithHexString:@"#535353"];
    [listSegmented addTarget:self action:@selector(changeValue:) forControlEvents:UIControlEventValueChanged];
    [self.view addSubview:listSegmented];
    */
    self.taoVC = [[taoCanVC alloc]init];
    self.anVC = [[anLiVC alloc]init];
    [self addChildViewController:self.taoVC];
    [self addChildViewController:self.anVC];
    [self.view addSubview:self.taoVC.view];
    [self.view addSubview:self.anVC.view];
    [self.taoVC release];
    [self.anVC release];
    [self.view bringSubviewToFront:self.taoVC.view];
//    listSegmented.selectedSegmentIndex = 0;
    
    segmentedControl.selectedSegmentIndex = 0;
    
}
/*
- (UIColor *) colorWithHexString: (NSString *) hexString {
    NSString *colorString = [[hexString stringByReplacingOccurrencesOfString: @"#" withString: @""] uppercaseString];
    CGFloat alpha, red, blue, green;
    switch ([colorString length]) {
        case 3: // #RGB
            alpha = 1.0f;
            red   = [self colorComponentFrom: colorString start: 0 length: 1];
            green = [self colorComponentFrom: colorString start: 1 length: 1];
            blue  = [self colorComponentFrom: colorString start: 2 length: 1];
            break;
        case 4: // #ARGB
            alpha = [self colorComponentFrom: colorString start: 0 length: 1];
            red   = [self colorComponentFrom: colorString start: 1 length: 1];
            green = [self colorComponentFrom: colorString start: 2 length: 1];
            blue  = [self colorComponentFrom: colorString start: 3 length: 1];
            break;
        case 6: // #RRGGBB
            alpha = 1.0f;
            red   = [self colorComponentFrom: colorString start: 0 length: 2];
            green = [self colorComponentFrom: colorString start: 2 length: 2];
            blue  = [self colorComponentFrom: colorString start: 4 length: 2];
            break;
        case 8: // #AARRGGBB
            alpha = [self colorComponentFrom: colorString start: 0 length: 2];
            red   = [self colorComponentFrom: colorString start: 2 length: 2];
            green = [self colorComponentFrom: colorString start: 4 length: 2];
            blue  = [self colorComponentFrom: colorString start: 6 length: 2];
            break;
        default:
            [NSException raise:@"Invalid color value" format: @"Color value %@ is invalid.  It should be a hex value of the form #RBG, #ARGB, #RRGGBB, or #AARRGGBB", hexString];
            break;
    }
    return [UIColor colorWithRed: red green: green blue: blue alpha: alpha];
}
-(void)changeValue:(id)sender
{
    CCSegmentedControl* segmentedControl = sender;
    
        switch (segmentedControl.selectedSegmentIndex) {
        case 0:
        {
            [self.view bringSubviewToFront:self.taoVC.view];
            [self.view bringSubviewToFront:segmentedControl];
            if (self.taoVC.TaoCanArray.count == 0) {
                UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"温馨提示" message:@"您的套餐收藏为空，快去收藏吧" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:nil, nil];
                [self.view addSubview:alert];
                [alert show];
                
            }

        }
            break;
        case 1:
        {
            [self.view bringSubviewToFront:self.anVC.view];
            [self.view bringSubviewToFront:segmentedControl];
            if (self.anVC.AnLiArray.count == 0) {
                UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"温馨提示" message:@"您的案例收藏为空，快去收藏吧" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:nil, nil];
                [self.view addSubview:alert];
                [alert show];
                //            [self.navigationController popViewControllerAnimated:YES];
            }
        }
            break;
        default:
            break;
    }
    
}
- (CGFloat) colorComponentFrom: (NSString *) string start: (NSUInteger) start length: (NSUInteger) length {
    NSString *substring = [string substringWithRange: NSMakeRange(start, length)];
    NSString *fullHex = length == 2 ? substring : [NSString stringWithFormat: @"%@%@", substring, substring];
    unsigned hexComponent;
    [[NSScanner scannerWithString: fullHex] scanHexInt: &hexComponent];
    return hexComponent / 255.0;
}
*/
-(void)segmentAction:(UISegmentedControl *)segmentControl{
    if (segmentControl.selectedSegmentIndex == 0) {
        [self.view bringSubviewToFront:self.taoVC.view];
        if (self.taoVC.TaoCanArray.count == 0) {
            UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"温馨提示" message:@"您的套餐收藏为空，快去收藏吧" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:nil, nil];
            [self.view addSubview:alert];
            [alert show];
            
        }

    }
    if (segmentControl.selectedSegmentIndex == 1) {
        [self.view bringSubviewToFront:self.anVC.view];
        if (self.anVC.AnLiArray.count == 0) {
            UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"温馨提示" message:@"您的案例收藏为空，快去收藏吧" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:nil, nil];
            [self.view addSubview:alert];
            [alert show];
//            [self.navigationController popViewControllerAnimated:YES];
        }

    }
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if(buttonIndex == 0){
    [self.navigationController popViewControllerAnimated:YES];
    }
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
